const app = getApp()

Component({
  properties: {
    text: {
      type: String,
      value: 'Wechat'
    },
    titleColor: {
      type: String,
      value: 'black',
      observer(newVal) {
        this.setData({
          fontColor: newVal
        })
      }
    },
    backColor: {
      type: String,
      value: 'white'
    },
    back: {
      type: Boolean,
      value: false
    },
    isOpacity: {
      type: Boolean,
      value: false
    },
    home: {
      type: Boolean,
      value: false
    }
  },
  data: {
    op: 0,
    fontColor: 'white',
    statusBarHeight: app.globalData.statusBarHeight + 'px',
    navigationBarHeight: app.globalData.statusBarHeight + 44 + 'px'
  },
  lifetimes: {
    attached() {
      this.setData({
        fontColor: this.properties.titleColor
      })
    }
  },
  methods: {
    onScroll(top) {
      const max = 40
      if (top >= max) {
        top = max
      }
      if (top >= 20) {
        this.setData({
          op: top / max,
          fontColor: 'black'
        })
      } else {
        this.setData({
          op: top / max,
          fontColor: this.properties.titleColor
        })
      }
    },
    backHome: function () {
      let pages = getCurrentPages()
      wx.switchTab({
        url: '/' + pages[0].route,
      })
    },
    back: function () {
      let pages = getCurrentPages()
      if (pages.length === 2) {
        wx.switchTab({
          url: '/' + pages[0].route,
        }).catch(() => {
          wx.navigateBack({
            delta: 1
          })
        })
      } else {
        wx.navigateBack({
          delta: 1,
          fail: (e) => {
            console.log(e);
          },
          success: (e) => {
            console.log('成功');
          }
        })
      }
    }
  }
})